package dao;

import utils.DBHelper;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderDao {

    public Map<String, Object> query(String status, String begin, String end, int page, int size){
        String sql = "select username, o.date, site, o.amount, o.`status` from `order` o, users u, orderitem i, address a, plist p where o.id=i.id and p.id=i.pid and o.uid=u.id and o.aid=a.id and o.status>-1 ";
        ArrayList<Object> params = new ArrayList<>();
        // 购买状态
        //if (status)
        if (status != null && status.trim().isEmpty() == false){
            sql += " and o.status = ?";
            params.add(status);
        }
        if (begin != null && begin.trim().isEmpty() == false){
            sql += " and date >= ?";
            params.add(Date.valueOf(begin));
        }
        if (end != null && end.trim().isEmpty() == false){
            sql += " and date <= ?";
            params.add(Date.valueOf(end));
        }
        long total = DBHelper.count(sql, params.toArray());
        sql += " limit ?, ?";
        params.add(size * (page - 1));
        params.add(size);
        List<Map<String, Object>> data = DBHelper.selectListMap(sql, params.toArray());
        Map<String, Object> res = new HashMap<>();
        res.put("total", total);
        res.put("data", data);
        return res;
    }

    public void remove(String id){
        String sql = "delete from order where id=?";
        DBHelper.update(sql, id);
    }

    public void remove1(String id){
        String sql = "update `order` set status=-1 where id=?";
        DBHelper.update(sql, id);
    }
}
